package com.wy.crl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.wy.model.AbsModel;
import com.wy.result.Result;
import com.wy.service.AbsService;
import com.wy.utils.ListUtils;
import com.wy.utils.MapUtils;
import com.wy.utils.StrUtils;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@SuppressWarnings("unchecked")
public abstract class AbsCrl<T> {

	public abstract AbsService<T> getService();

	@ApiOperation("单条数据新增")
	@PostMapping("create")
	public Result save(@RequestBody T model) {
		Object res = null;
		if (model instanceof AbsModel) {
			AbsModel<T> baseModel = (AbsModel<T>) model;
			if (baseModel.checkAdd()) {
				Map<String, Object> check = baseModel.checkAdd(model);
				if (check != null) {
					int code = Integer.parseInt(check.get("code").toString());
					if (2 == code) {
						String sortColumn = Objects.toString(check.get("sort"));
						res = getService().saveSort(model, sortColumn);
						return Objects.isNull(res) ? Result.ok(res) : Result.error("新增失败");
					}
				}
			}
		}
		res = getService().save(model);
		return Objects.isNull(res) ? Result.ok(res) : Result.error("新增失败");
	}

	@ApiOperation("批量数据新增,不检查任何有效性")
	@PostMapping("creates")
	public Result saves(@RequestBody List<T> models) {
		Object beans = getService().saves(models);
		return Objects.isNull(beans) ? Result.ok(beans) : Result.error("新增失败");
	}

	@ApiOperation("根据主键删除表中单条数据")
	@DeleteMapping("removeId/{id}")
	public Result delete(@PathVariable Serializable id) {
		int row = getService().delete(id);
		if (row > 0) {
			return Result.ok(row);
		}
		return Result.error("删除失败");
	}

	@ApiOperation("根据主键批量删除表中数据")
	@PostMapping("removeIds")
	public Result removeIds(@RequestBody List<Serializable> Ids) {
		if (ListUtils.isBlank(Ids)) {
			return Result.error("集合数据为空");
		}
		return Result.ok(getService().deletes(Ids));
	}

	@ApiOperation("根据主键更新表中的该条数据的全部字段,若是传null,则数据库中字段就为null")
	@PutMapping("edit")
	public Result modify(@RequestBody T model) {
		if (model instanceof AbsModel) {
			if (((AbsModel<T>) model).checkUpdate()) {
				Result check = ((AbsModel<T>) model).checkUpdate(model);
				if (check != null) {
					return check;
				}
			}
		}
		return Result.ok(getService().update(model));
	}

	@ApiOperation("查询该类中某个字段值是否重复,若有多个键值对,则只要有一个键值对中值重复,返回false")
	@PostMapping("hasValue")
	public Result hasValue(@RequestBody Map<String, Object> params) {
		if (MapUtils.isBlank(params)) {
			return Result.error("参数为空");
		}
		for (Map.Entry<String, Object> entry : params.entrySet()) {
			if (StrUtils.isNotBlank(entry.getKey())) {
				if (getService().hasValue(entry.getKey(), entry.getValue())) {
					return Result.ok(entry.getKey() + "字段值重复", 1);
				}
			}
		}
		return Result.ok("无重复值", 0);
	}

	@ApiOperation("根据主键获得数据详情")
	@GetMapping("getById/{id}")
	public Result getById(@ApiParam("数字主键编号") @PathVariable Serializable id) {
		return Result.ok(getService().getById(id));
	}

	@ApiOperation("查询单个表属性所有数据或分页数据,除分页参数,其他参数不可带")
	@GetMapping("getEntitys")
	public Result getEntitys(@ApiParam("页数") Integer pageIndex,
			@ApiParam("每页显示数目") Integer pageSize) {
		return Result.ok(getService().getEntitys(pageIndex, pageSize));
	}

	@ApiOperation("查询单个表属性所有数据或分页数据,可带参数")
	@GetMapping("getEntitysByCnd")
	public Result getEntitysByCnd(@ApiParam("该API实体类参数") T entity) {
		return getService().getEntitysByCnd(entity);
	}
}